/*
 * Copyright (C) 2009  Sylvain Maucourt (smaucourt@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *
 */
package net.sylvek.where.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

import java.util.Iterator;
import java.util.List;

public class Admin implements EntryPoint, ClickHandler {

	final HTMLTable table = new FlexTable();

	class RemoveButton extends Button {
		final String uid;

		RemoveButton(String uid) {
			super("remove");
			this.uid = uid;
		}
	}

	private final AdminServiceAsync adminService = GWT
			.create(AdminService.class);

	public void onModuleLoad() {
		initTable();
		initTitleTable();
		update();
	}

	private void initTable() {
		table.setBorderWidth(1);
		RootPanel.get("users").add(table);
	}

	private void initTitleTable() {
		table.setText(0, 0, "nom");
		table.setText(0, 1, "uid");
		table.setText(0, 2, "ip");
                table.setText(0, 3, "hash");
		table.setText(0, 4, "update");
		table.setText(0, 5, "remove");
	}

	private void clear() {
		for (int row = 1; row < table.getRowCount(); row++) {
			for (int column = 0; column < 5; column++) {
				table.clearCell(row, column);
			}
		}
	}

	private void update() {
		adminService.getUsers(new AsyncCallback<List<User>>() {

			public void onFailure(Throwable caught) {
				Window.alert("error during getting users");
			}

			@SuppressWarnings("deprecation")
			public void onSuccess(List<User> result) {

				int row = 1;
				Iterator<User> i = result.iterator();
				while (i.hasNext()) {
					User u = i.next();
					String id = u.getUid().replace("fb://", "");
					table.setText(row, 0, u.getName());
					table.setWidget(row, 1, new HTML(
							"<a href='http://www.facebook.com/#!/profile.php?id="
									+ id + "'>" + id + "</a>"));
					table.setText(row, 2, u.getIp());
					table.setText(row, 3, u.getHash());
					table.setText(row, 4, u.getUpdate().toGMTString());

					Button remove = new RemoveButton(u.getUid());
					remove.addClickHandler(Admin.this);

					table.setWidget(row, 5, remove);
					row++;
				}

				RootPanel.get("count").add(new Label("users : " + (row - 1)));
			}
		});
	}

	public void onClick(ClickEvent event) {
		RemoveButton b = (RemoveButton) event.getSource();
		adminService.delete(b.uid, new AsyncCallback<Boolean>() {

			public void onFailure(Throwable caught) {
				Window.alert("error during deleting user");
			}

			public void onSuccess(Boolean result) {
				if (!result) {
					Window.alert("error during deleting user");
				} else {
					clear();
					update();
				}
			}
		});
	}

}
